package com.kc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kc.bean.UserPassword;
import com.kc.mapper.UserPasswordMapper;
import com.kc.service.IUserPasswordService;
import com.kc.util.PasswordUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CompletableFuture;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-10-09
 */
@Service
public class UserPasswordServiceImpl extends ServiceImpl<UserPasswordMapper, UserPassword> implements IUserPasswordService {

    @Resource
    private UserPasswordMapper userPasswordMapperl;

    @Async
    @Override
    public CompletableFuture<Integer> inst(UserPassword userPassword) {
        int i = userPasswordMapperl.insert(userPassword);
        return CompletableFuture.completedFuture(i);
    }

    @Override
    public String getAccountId(String phone, String password) throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        StringBuilder builder = new StringBuilder();
        String newPassword = builder.append(phone).append("|").append(PasswordUtil.encryptAES(password.getBytes())).toString();
        String id = userPasswordMapperl.GetAccounId(Wrappers.<UserPassword>lambdaQuery().eq(UserPassword::getPhone,newPassword).or().eq(UserPassword::getEmail,newPassword));
        return id;
    }
}
